import urllib.request
import urllib.parse
import requests
from lxml import etree
import re
import json
import threading
import time
import openpyxl

locka = threading.Lock()
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
    # 'cookie': 'SECKEY_ABVK=WexAehAJs5LyFX4CDvaNtyZxARFG1i/XR4E1gBc8Sbw=; BMAP_SECKEY=wghxg0KrqNFQe2_vgv0WBhGj8hapMtEBeDt_wrRRFusJMaAAgMRwWsHH1MgTgqmA1HBdfgEV1-R5nNdAQPUXJmwq_jNRa3Wup5RK3Y5Hf1_MxnjfLMXKiks-f8MoL45D8PiKjQA-11EC3gICiLE-nUm7HHa6KTp6VMFxcr3UvE9NaWjiD4SKPUGvDu-PqdxY; __wpkreporterwid_=36059d98-39c3-479b-0ef4-9a097b9f7f7b; SECKEY_ABVK=WexAehAJs5LyFX4CDvaNtxtQfCJLOuSaizT+Ke4d9ps=; BMAP_SECKEY=wghxg0KrqNFQe2_vgv0WBnE6vshWP7uYK_8gQyyxW3gcmhFA9ierX9JV9jk0axsV0Ci1CUGLZ5D3BRAFxoahd9LXv2gA40paVY1QXHaqK-LcWx1gR7gtCLoSAW2sHD7-q_UZoyIemCZXM0K3aFQ0QSA36deQl3MQWQmRXZfFjg97-8XsB5-gYOYO2daNDJsy; gr_user_id=39c4b13a-bb99-4716-bdbc-1eb3752ef408; UM_distinctid=181c4a15ba112ea-04b71988f9224c-26021b51-144000-181c4a15ba23c0; methodType=undefined; parseLoginKey={"login_type":"1","phone":"18596361105","wx_uin":"","openid":"","mac":"a20eae5fb7838dbbbdd3ef0fe3494703","agent":"6","time":"1656861324","is_perfect":"1","random":"07n8t3AJ","pushcode":""}; originLoginKey=323a57d758c3d5e3662ebec7151c651d327c79999f8aa24cc2dcbba289bd89df6e1fba468733ec65f6514152d80b3f870dedef91cbab4f28bf5a99f39abab7ba3ee100b5c682f432230b9b890928245a45ddf78ddfdc3fff40283c717dcc10efacdbcf0413caad972c17401876978ddaf36e90e563ff96d4a9b1d68b64a807dba5091796161cdd357f065cadc0fa244ea7437ac7cd578dd6f5b86a8e04527b2c; 88025341dda01c5f_gr_last_sent_cs1=3D34B19414BB0B15FFA0823A70F726C9; __wpkreporterwid_=8d5b0ddd-ce19-4ae3-2f11-b9e54c33b81d; Hm_lvt_9b4517aa97b6b67e7c396bef15886cef=1656861318,1656924658,1657265742; Hm_lvt_17c8dee9c87e3ab669ce5dd4f88140ec=1656861318,1656924658,1657265742; 88025341dda01c5f_gr_session_id=df7a17ab-eadd-4be8-91a3-f4a2f3f03fa4; 88025341dda01c5f_gr_last_sent_sid_with_cs1=df7a17ab-eadd-4be8-91a3-f4a2f3f03fa4; 88025341dda01c5f_gr_session_id_df7a17ab-eadd-4be8-91a3-f4a2f3f03fa4=true; areaid=33; cityid=3301; CNZZDATA4696252=cnzz_eid=1656381861-1656859298-&ntime=1657263802; SECKEY_ABVK=WexAehAJs5LyFX4CDvaNtw2C45oFzGpcqbz7+lM3q7U=; BMAP_SECKEY=wghxg0KrqNFQe2_vgv0WBjke3DsdIpqrd5pfO_TGLMmWQg3R3KDFVlzGqrm7TG0OaZXl4ra3Az91AOx6yy5JitKhAabrOfyXhMoEYJhrFXP-9jF2UnZtGUBvj9RguR9A5vi5ybM3l8QhFih-zVu_-zeXlrtBqVdiCArQP0Gfu-3QvU-BnRW0KYLxbFRASV9q; Hm_lpvt_17c8dee9c87e3ab669ce5dd4f88140ec=1657265961; Hm_lpvt_9b4517aa97b6b67e7c396bef15886cef=1657265961; 88025341dda01c5f_gr_cs1=3D34B19414BB0B15FFA0823A70F726C9'
}

url = 'https://static-data.gaokao.cn/www/2.0/school/name.json'
res = requests.get(url=url, headers=headers)  # 这个获取我们的 name里面的
res2 = res.text
res2 = json.loads(res2)
school_list = res2['data']
for school in school_list:
    school_id = int(school['school_id'])
    print(school_id)
    base_url = f'https://static-data.gaokao.cn/www/2.0/school/{school_id}/detail/69000.json'
    data = requests.get(url=base_url, headers=headers).text
    data = json.loads(data)
    # tree = etree.HTML(data.text)
    # xpath = '//div[@class="intro_details_content xh-highlight"]//span/text()'
    # content = tree.xpath(xpath)
    try:
        data = data['data']['content']
        if len(data) != 0:
            # 进行保存我们的数据
            print("不为null")
    except TypeError:
        print("")

# https://static-data.gaokao.cn/www/2.0/school/3615/detail/69000.json
# intro_details_content xh-highlight
# https://static-data.gaokao.cn/www/2.0/school/3615/info.json
# //div[@class="intro_details_content"]
# 加上这句话我们才能使我们字符串转成我们的列表之后

# print(res2['data'])
# print(res2)
# 用这个可以将我们的数据进行解码（将我们的中文名字获取出来）
# str = '\\u5a04\\u5e95\\u5e7c\\u513f\\u5e08\\u8303\\u9ad8\\u7b49\\u4e13\\u79d1\\u5b66\\u6821'
# print(str.encode('utf8').decode('unicode_escape'))


# 学校的url地址 https://www.gaokao.cn/school/3490  其中的3490是可以进行替换的


# # 进入页面获取图片的地址 https://static-data.gaokao.cn/www/2.0/school/587/img/list.json  这个是获取图片的地址的数据
# url2 = 'https://static-data.gaokao.cn/www/2.0/school/589/img/list.json'
# img_json = requests.get(url=url2, headers=headers)
#
#
# # print(json.loads(img_json.text))
#
#
# # 其实我们只要是获取到我们的 学校的id之后我们的数据其实都是可以获取到的
# # https://static-data.gaokao.cn/upload/school/201908/1560005035_6240_thumb.jpg
#
#
# # https://static-data.gaokao.cn/www/2.0/school/name.json
# def real_save(shcool_name, title, url):
#     base_url = f'https://static-data.gaokao.cn{url}'
#     content = requests.get(url=base_url, headers=headers).content
#     with open('D:\\deleteAnyTime\\爬虫获取的数据\\大学详情信息图片\\' + shcool_name + title + '.jpg', mode='wb') as f:
#         f.write(content)
#         print(shcool_name + title, "保存成功")
#
#
# def get_res(url):
#     res = requests.get(url=url, headers=headers)
#     return res.text
#
#
# def save(name, id):
#     # 对线程上锁
#     base_url = f'https://static-data.gaokao.cn/www/2.0/school/{id}/img/list.json'
#     res = requests.get(url=base_url, headers=headers).text
#     try:
#         data_list = json.loads(res)['data']
#     except TypeError:
#         print('')
#     if data_list:
#         locka.acquire()
#         for data in data_list:
#             real_save(name, data['title'], data['url'])
#         locka.release()
#
#
# def get_logo_pic(school_list):
#     # 开启多线程（进行爬取）
#     print("线程开始")
#     threads = []
#     for school in school_id_list:
#         threads.append(
#             threading.Thread(target=save, args=(school['name'], school['school_id']))
#         )
#     for thread in threads:
#         thread.start()
#     for thread in threads:
#         thread.join()
#     pass


# if __name__ == '__main__':
#     start_time = time.time()
#     log_url = 'https://static-data.gaokao.cn/www/2.0/school/name.json'
#     res = get_res(log_url)
#     school_id_list = json.loads(res)['data']
#     get_logo_pic(school_id_list)
#     end_time = time.time()
#     print("用时", end_time - start_time)
#     pass


# <p>宿州航空职业学院是经安徽省人民政府批准，教育部备案，纳入统一招生计划的全日制高等职业院校。
# 学院位于宿州市宿马现代产业园，集“人文、国际、经典、科技”于一体的英伦校园，是泛美教育集团打造的又一艺术传奇。
# 学院航空实训中心设施先进，配置齐全。</p><p><br/></p><p>学院立足安徽、面向长三角、服务航空产业，培养高素质技术技能型人才，
# 初期开设无人机应用技术、民航运输服务、定翼机驾驶技术、空中乘务、民航安全技术管理、飞机机电设备维修、机场运行服务与管理、
# 人工智能技术应用、网络营销与直播电商等9个专业。</p><p><br/></p><p>学院采用集团化办学模式，集团拥有中国民航局授予的“民航客舱服务培训”
# 、“民航安全员培训”等资质，与民航机场、北京中软国际教育科技股份有限公司等建立有合作关系，毕业生将主要就业于飞行、空乘、空保、
# 无人机驾驶、飞机维修、安检、航空物流、贵宾服务、值机、AI数据工程师、互联网营销师等岗位。</p><p><br/></p>
